package com.pureut.order.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.order.domain.SubcontractingOrderMaterialDetail;
import com.pureut.order.dto.MaterialAwmateriaDto;
import com.pureut.order.dto.MaterialHalfDto;
import com.pureut.order.dto.SubcontractingOrderMaterialDetailDto;
import com.pureut.order.mapper.SubcontractingOrderMaterialDetailMapper;
import com.pureut.order.service.SubcontractingOrderMaterialDetailService;
import com.pureut.order.service.SubcontractingOrderService;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 *
 * Author:C
 * Date:2022/12/02 11:38
 * @DESC
 */
@Service
public class SubcontractingOrderMaterialDetailServiceImpl extends ServiceImpl<SubcontractingOrderMaterialDetailMapper, SubcontractingOrderMaterialDetail>
        implements SubcontractingOrderMaterialDetailService {

    @Resource
    SubcontractingOrderMaterialDetailMapper subcontractingOrderMaterialDetailMapper;

    @Resource
    SubcontractingOrderService subcontractingOrderService;

    @Resource
    FeignService feignService;

    /**
     * 委外订单物料明细列表
     *
     * @param id
     * @return
     */
    @Override
    public List<SubcontractingOrderMaterialDetailDto> getList(Long id) {

        //获取委外订单物料明细信息
        List<SubcontractingOrderMaterialDetailDto> subcontractingOrderMaterialDetailView = subcontractingOrderMaterialDetailMapper.getSubcontractingOrderMaterialDetailView(id);
        //获取单位字典数据
        List<SysDictData> unitArray = DictUtils.getDictCache("sys_material_unit");
        Map<String, String> unitMap = unitArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        for (SubcontractingOrderMaterialDetailDto entity : subcontractingOrderMaterialDetailView) {
            entity.setMaterialUnitDict(unitMap.get(String.valueOf(entity.getMaterialUnit())));

            // 原料：1
            if ("1".equals(entity.getMaterialType())) {
                //类型
                entity.setMaterialTypeDict("原料");
                //获取原料信息
                String materialCode = entity.getMaterialCode();
                AjaxResult awmateriaViewByMaterialCode = feignService.getAwmateriaViewByMaterialCode(materialCode);
                MaterialAwmateriaDto data = JSON.parseObject(JSON.toJSONString(awmateriaViewByMaterialCode.get("data")), MaterialAwmateriaDto.class);
                //类别
                entity.setMaterialCategoryDict(data.getMaterialCategory());
            }
            // 半成品：3
            if ("3".equals(entity.getMaterialType())) {
                //类型
                entity.setMaterialTypeDict("半成品");
                //获取半成品信息
                String materialCode = entity.getMaterialCode();
                AjaxResult halfViewByMaterialCode = feignService.getHalfViewByMaterialCode(materialCode);
                MaterialHalfDto data = JSON.parseObject(JSON.toJSONString(halfViewByMaterialCode.get("data")), MaterialHalfDto.class);
                //类别
                entity.setMaterialCategoryDict(data.getMaterialCategory());
            }
        }
        return subcontractingOrderMaterialDetailView;
    }
}
